#!/system/bin/sh
[[ $(id -u) -ne 0 ]] && echo "你是憨批？不给Root用你妈 爬" && exit 1
[[ -z $(echo ${0%/*} | grep -v 'mt') ]] && echo "草泥马不解压缩？用毛线 憨批" && exit 1
[[ ! -d ${0%/*}/tools ]] && echo "${0%/*}/tools目录遗失" && exit 1
. ${0%/*}/tools/bin.sh
#记录开始时间
starttime1=$(date +"%Y-%m-%d %H:%M:%S")
i=1
txt="${0%/*}/name.txt"
[[ ! -e $txt ]] && echo "$txt缺少" && exit 0
r=$(cat $txt | grep -v "#" | sed -e '/^$/d' | sed -n '$=')
[[ ! -z $r ]] && h=$r
#转换echo颜色提高可读性
echoRgb() {
	if [[ -n $2 ]]; then
		if [[ $3 == 1 ]]; then
			echo -e "\e[1;32m $1\e[0m"
		else
			echo -e "\e[1;31m $1\e[0m"
		fi
	else
		echo -e "\e[1;${bn}m $1\e[0m"
	fi
}
#显示执行结果
echo_log() {
	if [[ $? == 0 ]]; then
		echoRgb "$1成功" "0" "1"
	else
		echoRgb "$1恢复失败，过世了" "0" "0"
	fi
}
#计算结束时间
endtime() {
	#计算总体切换时长耗费
	case $1 in
	1) starttime=$starttime1 ;;
	2) starttime=$starttime2 ;;
	esac
	endtime=$(date "+%Y-%m-%d %H:%M:%S")
	duration=$(echo $(($(date +%s -d "${endtime}") - $(date +%s -d "${starttime}"))) | awk '{t=split("60 秒 60 分 24 時 999 天",a);for(n=1;n<t;n+=2){if($1==0)break;s=$1%a[n]a[n+1]s;$1=int($1/a[n])}print s}')
	[[ -n $duration ]] && echoRgb "$2用時:$duration" || echoRgb "$2用時:0秒"
}

[[ ! $(getenforce) == Permissive ]] && setenforce 0 && echoRgb "selinux關閉"
bn=37
#开始循环$txt内的资料进行恢复
while [[ $i -le $h ]]; do
	#let bn++
	#[[ $bn -ge 37 ]] && bn=31
	echoRgb "恢复第$i个应用 总共$h个 剩下$(($h - $i))个应用"
	name=$(cat $txt | grep -v "#" | sed -e '/^$/d' | sed -n "${i}p" | awk '{print $2}')
	name2=$(cat $txt | grep -v "#" | sed -e '/^$/d' | sed -n "${i}p" | awk '{print $1}')	
	[[ -z $name ]] && echoRgb "警告! name.txt软件包名获取失败，可能修改有问题" "0" "0" && exit 1
	if [[ -n $name && -d ${0%/*}/$name ]]; then
	    echoRgb "恢复$name2 ($name)"
    	starttime2=$(date +"%Y-%m-%d %H:%M:%S")
		zb=$(pm list packages | grep -w "$name" | sed 's/package://g')
		if [[ $(find "${0%/*}/$name" -maxdepth 1 -name "*.apk" -type f | wc -l) == 1 ]]; then
			if [[ -z $zb ]]; then
				echoRgb "$name2是常规Apk 开始安装"
				[[ -e ${0%/*}/$name/nmsl.apk ]] && pm install -r -g "${0%/*}/$name/nmsl.apk" >/dev/null 2>&1
				pm install -r -g "$(find ${0%/*}/$name -name "*.apk" -type f)" >/dev/null 2>&1
				echo_log "Apk安装"
			else
				echoRgb "$name2存在当前系统中 略过安装Apk"
			fi
		else
			if [[ -z $zb ]]; then
				echoRgb "$name2是多分包Apk 开始安装"
				b=$(echo $(pm install-create) | grep -E -o '[0-9]+')
				[[ -e ${0%/*}/$name/nmsl.apk ]] && pm install -r -g "${0%/*}/$name/nmsl.apk" >/dev/null 2>&1
				find "${0%/*}/$name" -maxdepth 1 -name "*.apk" -type f | while read i; do
					ad=$(basename $i | grep -v 'nmsl.apk')
					pm install-write $b $ad ${i%/*}/$ad >/dev/null 2>&1
					echo_log "$(basename "$i")安裝"
				done
				pm install-commit $b
				echo_log "split Apk安装"
			else
				echoRgb "$name2存在当前系统中 略过安装Apk"
			fi		
		fi
		if [[ -n $(pm list packages | grep -w "$name" | sed 's/package://g') ]]; then
			#停止软件
			[[ ! $name == bin.mt.plus && ! $name == com.termux && ! $name == com.mixplorer.silver ]] && am force-stop $name
			echoRgb "$name2存在 开始释放数据"
			if [[ $(cat /data/system/packages.list | grep -w "$name" | awk '{print $1}') == $name ]]; then
				[[ -n $name ]] && G=$(cat /data/system/packages.list | grep -w "$name" | awk '{print $2}')
				X=$(cat /data/system/packages.list | grep -w "$name" | awk '{print $4}')
				if [[ -n $name ]]; then
					find ${0%/*}/$name -name "*.tar.zst" -o -name "*.tar.lz4" -type f | while read i; do
						case $(basename $i) in
						*-user.tar.zst)
							[[ -d /data/user/0/$name ]] && rm -rf /data/user/0/$name
							pv $i | tar -I zstd -xPpf -
							echo_log "$(basename $i)解压缩"
							;;
					    *-user.tar.lz4)
							[[ -d /data/user/0/$name ]] && rm -rf /data/user/0/$name
							pv $i | tar xPpf -
							echo_log "$(basename $i)解压缩"
							;;					        							
						*-data.tar.zst)
							[[ -d /sdcard/Android/data/$name ]] && rm -rf /sdcard/Android/data/$name
							pv $i | tar -I zstd -xPpf -                   
							echo_log "$(basename $i)解压缩"
							;;
					    *-data.tar.lz4)
							[[ -d /sdcard/Android/data/$name ]] && rm -rf /sdcard/Android/data/$name
							pv $i | tar xPpf -
							echo_log "$(basename $i)解压缩"
							;;					        							
						*-obb.tar.zst)
							[[ -d /sdcard/Android/obb/$name ]] && rm -rf /sdcard/Android/obb/$name
							pv $i | tar -I zstd -xPpf -                
							echo_log "$(basename $i)解压缩"
							;;
					    *-obb.tar.lz4)
							[[ -d /sdcard/Android/obb/$name ]] && rm -rf /sdcard/Android/obb/$name
							pv $i | tar xPpf -
							echo_log "$(basename $i)解压缩"
							;;					        
						esac
					done
				fi
				if [[ -d $X && -n $G ]]; then
					chown -R $G:$G $X
					echoRgb "路径$X  权限:$G:$G"
					echoRgb "$name2完成数据恢复"
				fi
			fi
		fi
		endtime 2 "$name2恢复"
		echoRgb
	else
        echoRgb "$name2资料夹遗失，无法恢复" "0" "0"
	fi
	let i++
done

#setenforce 1
echoRgb "批量恢复完成"
endtime 1 "批量恢复开始到结束"